package com.fenqiyong.www.action;

import android.content.Context;

import com.orhanobut.logger.Logger;
import com.tougc.bean.beans.MineSdkBean;
import com.tougc.bean.beans.SuccesErrorBean;
import com.tougc.bean.beans.UserBean;

import java.util.HashMap;

import javax.inject.Inject;

import io.reactivex.Observable;
import ren.nearby.http.request.BaseObserver;
import ren.nearby.http.request.HttpApi;
import ren.nearby.http.request.InstanceSucces;
import ren.nearby.http.request.SchedulersCompat;
import ren.nearby.http.utils.util.PreferencesUtils;
import ren.nearby.http.utils.util.ReqMessage;
import ren.nearby.http.utils.util.RxDisposble;
import ren.nearby.http.view.progress.DialogRequest;

/**
 * Created by Administrator on 2018/5/18 0018.
 */

public class FragmentMinePresent implements FragmentMineContract.Action, InstanceSucces {
    FragmentMineContract.View mView;  // 需要抽象出来
    @Inject
    HttpApi mApi;
    @Inject
    Context context;

    @Inject
    FragmentMinePresent() {
    }

    @Override
    public void takeView(FragmentMineContract.View view) {
        mView = view;
    }

    @Override
    public void dropView() {
        mView = null;
        RxDisposble.getInstance().unSubscribe();
    }

    @Override
    public void R0008(int userId) {


        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("userId", userId);
            final String message = ReqMessage.getInstance().message(map, context);
            Logger.e("message = " + message);
//                Observable ob = mApi.testRent(message);
            Observable ob = mApi.R0008(message);
            BaseObserver observer = new BaseObserver<UserBean>(mView.getContext()) {


                @Override
                public void onComplete() {
                    super.onComplete();
                }

                @Override
                public void onNext(UserBean user) {
                    PreferencesUtils.putString(context, "reality_name", user.getRealityName());
                    PreferencesUtils.putString(context, "compress_" + user.getMobile(), HttpApi.BASE_URL + "head/view.do?picid=" + user.getPicid());
                    PreferencesUtils.putString(context, "mobile", user.getMobile());
                    PreferencesUtils.putFloat(context, "frozen_balance", user.getFrozenBalance());
                    PreferencesUtils.putFloat(context, "unsettled_balance", user.getUnsettledBalance());
                    PreferencesUtils.putFloat(context, "user_balance", user.getUserBalance());
                    PreferencesUtils.putFloat(context, "cash_balance", user.getCashBalance());
                    if (user.getStatus() == null) {
                        Logger.e("null");
                        PreferencesUtils.putInt(context, "status", -1);
                    } else {
                        Logger.e("not null");
                        PreferencesUtils.putInt(context, "status", user.getStatus());
                    }
                    mView.start(user);
                }
            };
            ob.compose(SchedulersCompat.IO_TRANSFORMER)//处理线程
//                    .compose(RxResultHelper.handleResult())
                    .compose(SchedulersCompat.applyProgress(mView.getContext(), DialogRequest.TAG_REQUEST))//compose必须顺序要写对，得在handleResult后面
                    .subscribe(observer);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void R0031(int userId) {


        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("userId", userId);
            final String message = ReqMessage.getInstance().message(map, context);
            Logger.e("message = " + message);
//                Observable ob = mApi.testRent(message);
            Observable ob = mApi.R0031(message);
            BaseObserver observer = new BaseObserver<MineSdkBean>(mView.getContext()) {


                @Override
                public void onComplete() {
                    super.onComplete();
                }

                @Override
                public void onNext(MineSdkBean bean) {
                    mView.setMineSdkBean(bean);
                }
            };
            ob.compose(SchedulersCompat.IO_TRANSFORMER)//处理线程
//                    .compose(RxResultHelper.handleResult())
                    .compose(SchedulersCompat.applyProgress(mView.getContext(), DialogRequest.TAG_REQUEST))//compose必须顺序要写对，得在handleResult后面
                    .subscribe(observer);


        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void R0032(int userId, String headView) {


        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("userId", userId);
            map.put("headView", headView);
            final String message = ReqMessage.getInstance().message(map, context);
            Logger.e("message = " + message);
//                Observable ob = mApi.testRent(message);
            Observable ob = mApi.R0032(message);
            BaseObserver observer = new BaseObserver<SuccesErrorBean>(mView.getContext()) {


                @Override
                public void onComplete() {
                    super.onComplete();
                }

                @Override
                public void onNext(SuccesErrorBean bean) {

                }
            };
            ob.compose(SchedulersCompat.IO_TRANSFORMER)//处理线程
//                    .compose(RxResultHelper.handleResult(this))
                    .compose(SchedulersCompat.applyProgress(mView.getContext(), DialogRequest.TAG_REQUEST))//compose必须顺序要写对，得在handleResult后面
                    .subscribe(observer);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void succes(String msg) {
        mView.showSuccess();
    }
}


